/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test;

import common.Client;
import common.ReceiveHandleLoop;
import common.packet.Authorization;
import common.packet.ChatMessage;
import common.packet.OK;
import common.packet.Packet;
import common.packet.PacketHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xissburg
 */
public class ClientMain
{
    public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException, ClassNotFoundException
    {
        Socket socket = new Socket("localhost", 4004);
        final Client client = new Client(socket);
        client.write(new Authorization("dude"));

        final ReceiveHandleLoop receiveHandleLoop = new ReceiveHandleLoop(client);

        receiveHandleLoop.setErrorHandler(new ReceiveHandleLoop.ErrorHandler() {

            public boolean onSocketTimeoutException(SocketTimeoutException ex) {
                System.out.println("Socket timeout exception");
                return false;
            }

            public boolean onIOException(IOException ex) {
                System.out.println("IO exception");
                return false;
            }
        });

        receiveHandleLoop.addPacketHandler(new ErrorHandler(receiveHandleLoop));

        receiveHandleLoop.addPacketHandler(new PacketHandler(){
            public void handle(Packet packet) {
                receiveHandleLoop.removePacketHandler(getPacketCode());
                receiveHandleLoop.addPacketHandler(new ChatMessageHandler());
                try {
                    client.write(new OK("lol"));
                } catch (IOException ex) {
                    Logger.getLogger(ClientMain.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public Packet.Code getPacketCode() {
                return Packet.Code.OK;
            }
        });
        

        new Thread(receiveHandleLoop).start();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String line = null;

        while((line = in.readLine()) != null)
        {
            client.write(new ChatMessage(line));
        }
    }

    public static class ChatMessageHandler implements PacketHandler
    {
        public void handle(Packet packet)
        {
            ChatMessage chatMessage = (ChatMessage)packet;
            System.out.println(chatMessage.getMessage());
        }

        public Packet.Code getPacketCode()
        {
            return Packet.Code.CHAT_MESSAGE;
        }
    }

    public static class ErrorHandler implements PacketHandler
    {
        private ReceiveHandleLoop receiveHandleLoop;

        public ErrorHandler(ReceiveHandleLoop receiveHandleLoop) {
            this.receiveHandleLoop = receiveHandleLoop;
        }

        public void handle(Packet packet)
        {
            Error error = (Error)packet;
            System.out.println(error.getMessage());
            try {
                receiveHandleLoop.stop();
            } catch (IOException ex) {
                Logger.getLogger(ClientMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        public Packet.Code getPacketCode()
        {
            return Packet.Code.ERROR;
        }
    }
}
